package com.example.city_home_app.RecyclerViewAdapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.city_home_app.Activity.MomentInfoActivity;
import com.example.city_home_app.Activity.PostAddActivity;
import com.example.city_home_app.Bean.Down_Post;
import com.example.city_home_app.Bean.Down__Image;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Adapter_PostAdd extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<Down__Image> list;
    private int activityname;

    public Adapter_PostAdd(Context context,int activityname) {
        this.context = context;
        this.activityname = activityname;
    }

    public void setList(List<Down__Image> list) {
        this.list = list;
    }

    public void clearList(){
        this.list.clear();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.recitem_postimage, parent, false);
        Adapter_PostAdd.ViewHolder viewHolder = new Adapter_PostAdd.ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final Down__Image bean = list.get(position);
        if(bean == null){
            return;
        }
        final Adapter_PostAdd.ViewHolder viewHolder = (Adapter_PostAdd.ViewHolder) holder;
        Picasso.with(context)
                .load("http://siminzhang.top:8088/city_home_server/public/static/posters/"+bean.getImg())
                .into(viewHolder.imgRecitemPostimgImg);

        viewHolder.imgRecitemPostimgDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(activityname == 1) {
                    PostAddActivity.instance.deleteimg(position);
                }else{
                    MomentInfoActivity.instance.deleteimg(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_recitem_postimg_img)
        ImageView imgRecitemPostimgImg;
        @BindView(R.id.img_recitem_postimg_delete)
        ImageView imgRecitemPostimgDelete;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
